/*
 * Created by JFormDesigner on Tue Mar 29 22:27:12 CST 2022
 */

package com.wangcanxuan.www.view.tribe;

import javax.swing.event.*;

import com.wangcanxuan.www.controller.Impl.TribeControllerImpl;
import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.TribeController;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.Tribe;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

import static com.wangcanxuan.www.entity.dto.ResultConstants.SUCCESS_REQUEST_CODE;

/**
 * 提升部落界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class BenefitTribeView extends JFrame {
    private final TribeController tribeController;
    private final UserController userController;
    private final User user;
    private int coinsToCoins = 1;
    private int coinsToPower = 10;

    public BenefitTribeView(User user) {
        this.user = user;
        tribeController = ControllerProxy.getProxyInstance(TribeControllerImpl.class);
        userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);
        initComponents();
        setVisible(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        Tribe tribe = freshTribe();
        coinsText.setText(String.valueOf(tribe.getCoins()));
        userCoinsText.setText(String.valueOf(user.getCoins()));
        levelText.setText(String.valueOf(tribe.getTribeLevel()));
        powerText.setText(String.valueOf(tribe.getCombatPower()));
    }


    private void backToTribe(ActionEvent e) {
        // TODO add your code here
        new TribalInterfaceView(freshUser(), freshTribe());
        dispose();
    }

    private Tribe freshTribe() {
        ResultInfo<Tribe> tribeResultMessage = tribeController.selectTribeByTribeId(user.getTribeId());
        return tribeResultMessage.getObj();
    }

    private User freshUser() {
        ResultInfo<User> refresh = userController.refresh(user.getId());
        return refresh.getObj();
    }

    private void coinsToCoinsSpinnerStateChanged(ChangeEvent e) {
        // TODO add your code here
        coinsToCoins = (int) ((JSpinner) e.getSource()).getValue();
    }

    private void giveCoins(ActionEvent e) {
        // TODO add your code here
        if (coinsToCoins > user.getCoins()) {
            JOptionPane.showMessageDialog(null, "不要狮子开大口！");
            return;
        }
        ResultInfo<?> resultMessage = tribeController.coinToCoins(user, coinsToCoins);
        String message = resultMessage.getMessage();
        int code = resultMessage.getCode();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new BenefitTribeView(freshUser());
            dispose();
        }
    }

    private void coinsToPowerSpinnerStateChanged(ChangeEvent e) {
        // TODO add your code here
        coinsToPower = (int) ((JSpinner) e.getSource()).getValue();
    }

    private void button3(ActionEvent e) {
        // TODO add your code here
        int maxRoleId = 4;
        if (user.getRoleId() < maxRoleId) {
            JOptionPane.showMessageDialog(null, "不要私自动部落的钱钱噢！");
            return;
        }
        Tribe tribe = freshTribe();
        if (coinsToPower > tribe.getCoins()) {
            JOptionPane.showMessageDialog(null, "不要狮子开大口！");
            return;
        }
        ResultInfo<?> resultMessage = tribeController.coinToPower(tribe, coinsToPower);
        String message = resultMessage.getMessage();
        int code = resultMessage.getCode();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new BenefitTribeView(freshUser());
            dispose();
        }
    }

    private void levelUp(ActionEvent e) {
        // TODO add your code here
        int maxRoleId = 5;
        if (user.getRoleId() != maxRoleId) {
            JOptionPane.showMessageDialog(null, "你又不是首领升什么级？！");
            return;
        }
        Tribe tribe = freshTribe();
        int levelUpNum = 1000;
        int levelUpPower = (tribe.getTribeLevel() + 1) * 1000;
        if (levelUpPower > tribe.getCombatPower()) {
            JOptionPane.showMessageDialog(null, "凡事先看看自己的部落战力配不配！");
            return;
        }
        if (levelUpNum > tribe.getCoins()) {
            JOptionPane.showMessageDialog(null, "没钱你好意思升级？！");
            return;
        }
        ResultInfo<?> resultMessage = tribeController.levelUp(freshTribe());
        JOptionPane.showMessageDialog(null, resultMessage.getMessage());
        new BenefitTribeView(freshUser());
        dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        backToTribeButton = new JButton();
        coinsToCoinsSpinner = new JSpinner();
        giveCoinsButton = new JButton();
        label2 = new JLabel();
        userCoinsText = new JTextField();
        label3 = new JLabel();
        coinsText = new JTextField();
        label4 = new JLabel();
        label5 = new JLabel();
        powerText = new JTextField();
        coinsToPowerSpinner = new JSpinner();
        button3 = new JButton();
        label6 = new JLabel();
        levelText = new JTextField();
        label7 = new JLabel();
        levelUpButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u90e8\u843d\u63d0\u5347");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));

        //---- backToTribeButton ----
        backToTribeButton.setText("\u8fd4\u56de\u90e8\u843d");
        backToTribeButton.addActionListener(e -> backToTribe(e));

        //---- coinsToCoinsSpinner ----
        coinsToCoinsSpinner.setModel(new SpinnerNumberModel(1, 1, null, 1));
        coinsToCoinsSpinner.addChangeListener(e -> coinsToCoinsSpinnerStateChanged(e));

        //---- giveCoinsButton ----
        giveCoinsButton.setText("\u8d21\u732e\u91d1\u5e01");
        giveCoinsButton.addActionListener(e -> giveCoins(e));

        //---- label2 ----
        label2.setText("\u5f53\u524d\u91d1\u5e01\uff1a");

        //---- userCoinsText ----
        userCoinsText.setEditable(false);

        //---- label3 ----
        label3.setText("\u90e8\u843d\u91d1\u5e01\uff1a");

        //---- coinsText ----
        coinsText.setEditable(false);

        //---- label4 ----
        label4.setText("10\u91d1\u5e01\u63621\u6218\u529b");
        label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));

        //---- label5 ----
        label5.setText("\u5f53\u524d\u90e8\u843d\u6218\u529b\uff1a");

        //---- powerText ----
        powerText.setEditable(false);

        //---- coinsToPowerSpinner ----
        coinsToPowerSpinner.setModel(new SpinnerNumberModel(10, 10, null, 10));
        coinsToPowerSpinner.addChangeListener(e -> coinsToPowerSpinnerStateChanged(e));

        //---- button3 ----
        button3.setText("\u4ea4\u6362\u6218\u529b");
        button3.addActionListener(e -> button3(e));

        //---- label6 ----
        label6.setText("\u90e8\u843d\u7b49\u7ea7\uff1a");

        //---- levelText ----
        levelText.setEditable(false);

        //---- label7 ----
        label7.setText("\u6218\u529b\u8fbe\u52301000\u7684\u500d\u6570\u53ef\u5347\u7ea7\uff0c\u4e00\u6b21\u5347\u7ea7\u6d88\u80171000\u91d1\u5e01");

        //---- levelUpButton ----
        levelUpButton.setText("\u5347\u7ea7");
        levelUpButton.addActionListener(e -> levelUp(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(241, 241, 241)
                                                .addComponent(userCoinsText, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
                                                .addGap(125, 125, 125))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGap(98, 98, 98)
                                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                                        .addComponent(label7, GroupLayout.PREFERRED_SIZE, 325, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(levelUpButton, GroupLayout.PREFERRED_SIZE, 286, GroupLayout.PREFERRED_SIZE)
                                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                                                                        .addComponent(coinsToPowerSpinner, GroupLayout.Alignment.LEADING)
                                                                                        .addComponent(label5, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE))
                                                                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                                        .addComponent(powerText)
                                                                                        .addComponent(button3, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE)))
                                                                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
                                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                                                        .addComponent(label6, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE))
                                                                                .addGap(44, 44, 44)
                                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                                        .addComponent(levelText, GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                                                                                        .addComponent(coinsText, GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)))))
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                                .addGap(98, 98, 98)
                                                                                .addComponent(coinsToCoinsSpinner, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE))
                                                                        .addComponent(backToTribeButton, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))
                                                                .addGap(18, 18, 18)
                                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(giveCoinsButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                                                .addGap(92, 92, 92))))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(backToTribeButton, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
                                .addGap(28, 28, 28)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(coinsText, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))
                                .addGap(41, 41, 41)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(levelText, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label6, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))
                                .addGap(30, 30, 30)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(userCoinsText, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))
                                .addGap(44, 44, 44)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(coinsToCoinsSpinner, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(giveCoinsButton, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
                                .addGap(11, 11, 11)
                                .addComponent(label4, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label5, GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                                        .addComponent(powerText, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(coinsToPowerSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(29, 29, 29)
                                .addComponent(label7, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(levelUpButton)
                                .addGap(70, 70, 70))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JButton backToTribeButton;
    private JSpinner coinsToCoinsSpinner;
    private JButton giveCoinsButton;
    private JLabel label2;
    private JTextField userCoinsText;
    private JLabel label3;
    private JTextField coinsText;
    private JLabel label4;
    private JLabel label5;
    private JTextField powerText;
    private JSpinner coinsToPowerSpinner;
    private JButton button3;
    private JLabel label6;
    private JTextField levelText;
    private JLabel label7;
    private JButton levelUpButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
